import numpy as np
import tensorflow as tf
import pandas as pd
import sklearn
from sklearn.metrics import confusion_matrix
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from tensorflow import keras
from numpy import genfromtxt
import tensorflow_addons as tfa
from tensorflow.keras import regularizers


train_df = pd.read_csv('training_set_path', sep=',')
val_df = pd.read_csv('val_set_path', sep=',')
test_df = pd.read_csv('test_set_path', sep=',')
train_labels = np.array(train_df.pop('winner'))
val_labels = np.array(val_df.pop('winner'))
test_labels = np.array(test_df.pop('winner'))
bool_train_labels = train_labels != 0
train_features = np.array(train_df)
val_features = np.array(val_df)
test_features = np.array(test_df)

scaler = StandardScaler()
train_features = scaler.fit_transform(train_features)
val_features = scaler.transform(val_features)
test_features = scaler.transform(test_features)

METRICS = [
      keras.metrics.TruePositives(name='tp'),
      keras.metrics.FalsePositives(name='fp'),
      keras.metrics.TrueNegatives(name='tn'),
      keras.metrics.FalseNegatives(name='fn'), 
      keras.metrics.Precision(name='precision'),
      keras.metrics.Recall(name='recall'),
      keras.metrics.AUC(name='auc',curve='PR'),
]

initial_bias = np.log([pos/neg])

model_deep = keras.Sequential(
    [keras.layers.Dense(256, activation="relu",
                        input_shape=(train_features.shape[-1],)),
        keras.layers.Dropout(0.4),
        keras.layers.Dense(128,activation="relu"),
        keras.layers.Dropout(0.4),
        keras.layers.Dense(128,activation="relu"),
        keras.layers.Dropout(0.6),
        keras.layers.Dense(1, activation="sigmoid",bias_initializer=tf.keras.initializers.Constant(initial_bias)),
    ]
)
model_deep.summary()

loss_fnc=tfa.losses.SigmoidFocalCrossEntropy(
    alpha= 0.9999,
    gamma= 3
)

def make_model(metrics=METRICS):
    model = model_deep
    model.compile(
    optimizer=tf.keras.optimizers.Adadelta(learning_rate=0.1, rho=0.95, epsilon=1e-07),
    loss=loss_fnc,
    metrics=metrics)
    return model

model_nlp = make_model()

train_history=model_nlp.fit(
    train_features,
    train_labels,
    batch_size=8192,
    epochs=1000,
    verbose=2,
    callbacks=callbacks,
    validation_data=(val_features, val_labels)
)


